package club.babyf.nativeai.entity;

import club.babyf.nativeai.utils.JsonCustomUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.fasterxml.jackson.databind.type.TypeFactory;

import java.util.HashMap;
import java.util.List;

/**
 * CustomHashMap.
 *
 * @author zhanglei.
 * @date 2025/2/27 10:31.
 * @description 自定义HashMap.
 */
public class CustomHashMap<K, V> extends HashMap<K, V> {

    public CustomHashMap() {
        super();
    }

    public String getString(Object key) {
        V v = super.get(key);
        if (v == null) {
            return null;
        }
        return v.toString();
    }

    public Integer getInteger(Object key) {
        V v = super.get(key);
        if (v == null) {
            return null;
        }
        return Integer.parseInt(v.toString());
    }

    public Long getLong(Object key) {
        V v = super.get(key);
        if (v == null) {
            return null;
        }
        return Long.parseLong(v.toString());
    }

    public Double getDouble(Object key) {
        V v = super.get(key);
        if (v == null) {
            return null;
        }
        return Double.parseDouble(v.toString());
    }

    public Float getFloat(Object key) {
        V v = super.get(key);
        if (v == null) {
            return null;
        }
        return Float.parseFloat(v.toString());
    }

    public Boolean getBoolean(Object key) {
        V v = super.get(key);
        if (v == null) {
            return null;
        }
        return Boolean.parseBoolean(v.toString());
    }

    public <T> T getObject(Object key, Class<T> valueType) {
        V v = super.get(key);
        if (v == null) {
            return null;
        }
        // 通过JsonUtil工具类进行反序列化
        try {
            return JsonCustomUtil.getMapper().readValue(JsonCustomUtil.toJsonString(v), valueType);
        } catch (JsonProcessingException e) {
            throw new RuntimeException("JSON序列化失败", e);
        }
    }

    public <T> T getObject(Object key, TypeReference<T> valueTypeRef) {
        V v = super.get(key);
        if (v == null) {
            return null;
        }
        // 通过JsonUtil工具类进行反序列化
        try {
            return JsonCustomUtil.getMapper().readValue(JsonCustomUtil.toJsonString(v), valueTypeRef);
        } catch (JsonProcessingException e) {
            throw new RuntimeException("JSON序列化失败", e);
        }
    }

    public <T> List<T> getList(Object key, Class<T> valueType) {
        V v = super.get(key);
        if (v == null) {
            return null;
        }
        // 使用Class参数构造明确的集合类型
        CollectionType collectionType = JsonCustomUtil.getMapper().getTypeFactory().constructCollectionType(List.class, valueType);
        // 通过JsonUtil工具类进行反序列化
        try {
            return JsonCustomUtil.getMapper().readValue(JsonCustomUtil.toJsonString(v), collectionType);
        } catch (JsonProcessingException e) {
            throw new RuntimeException("JSON序列化失败", e);
        }
    }

    public <T> List<T> getList(Object key, TypeReference<T> valueTypeRef) {
        V v = super.get(key);
        if (v == null) {
            return null;
        }
        // 获取类型工厂
        TypeFactory typeFactory = JsonCustomUtil.getMapper().getTypeFactory();
        // 使用TypeReference参数构造明确的集合类型
        JavaType type = typeFactory.constructType(valueTypeRef);
        // 使用Class参数构造明确的集合类型
        JavaType collectionType = typeFactory.constructCollectionType(List.class, type);
        // 通过JsonUtil工具类进行反序列化
        try {
            return JsonCustomUtil.getMapper().readValue(JsonCustomUtil.toJsonString(v), collectionType);
        } catch (JsonProcessingException e) {
            throw new RuntimeException("JSON序列化失败", e);
        }
    }

}
